package com.example.onlinemusic.controller;


import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.service.UserService;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {

    @Resource
    private UserService userService;

    @RequestMapping("/insertlikemusic")
    public ResponseBodyMessage<Boolean> LoveMusic(@RequestParam String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);

        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            return new ResponseBodyMessage<>(-1, "您还没有登录 !", false);
        } else {
            User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
            int userId = user.getId();

            //TODO：还可以扩展，该音乐是否已经上传，只有上传成功的音乐才能收藏

            //先查询该音乐是否已经被收藏
            Music music = userService.findLoveMusicByMusicIdAndUserId(userId, musicId);
            if (music != null) {
                System.out.println("该音乐已经被收藏 !");
                return new ResponseBodyMessage<>(-1, "该音乐已经被收藏,重复收藏失败 !", false);
            } else {
                //没有收藏过就可以进行收藏操作
                Boolean flg = userService.insertLoveMusic(userId, musicId);
                if (flg) {
                    return new ResponseBodyMessage<>(1, "收藏音乐成功 !", true);
                } else {
                    return new ResponseBodyMessage<>(-1, "收藏音乐失败 !", false);
                }
            }
        }
    }


    //查找收藏的音乐：1.根据用户id，和关键字 进行模糊查询 2.根据用户id，不传入音乐名称，将所有收藏的音乐查询出来
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest req) {
        HttpSession httpSession = req.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("您还没有登录！");
            return new ResponseBodyMessage<>(-1, "您还没有登录 !", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userid = user.getId();
        List<Music> musicList = null;
        //根据用户id，和关键字 进行模糊查询
        if (musicName != null) {
            musicList = userService.findLoveMusicBykeyAndUID(musicName, userid);
        } else {
            // 根据用户id，不传入音乐名称，将所有收藏的音乐查询出来
            musicList = userService.findLoveMusicByUserId(userid);
        }
        return new ResponseBodyMessage<>(0, "查询到了您收藏的音乐", musicList);
    }


    // 根据用户id 和 音乐id来删除收藏的音乐
    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id, HttpServletRequest req) {
        int musicId = Integer.parseInt(id);
        HttpSession httpSession = req.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("您还没有登录 !！");
            return new ResponseBodyMessage<>(-1, "您还没有登录 !", false);
        }else {
            User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
            int userId = user.getId();
            int ret = userService.deleteLoveMusic(userId, musicId);
            if (ret == 1) {
                return new ResponseBodyMessage<>(1, "取消收藏成功！", true);
            } else {
                return new ResponseBodyMessage<>(1, "取消收藏失败！", false);
            }
        }
    }
}
